package muilty_thread.java_current_practice_and_kernel.cp6;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class TaskExecutionWebServer {
    private static final int NTHREADS = 100;
    private static final Executor exec = Executors.newFixedThreadPool(NTHREADS);

    public static void main(String[] args) throws IOException {
        ServerSocket sc = new ServerSocket(80);
        while (true) {
            Socket accept = sc.accept();
            exec.execute(() ->{
                handleRequest(accept);
            });
        }
    }


    public static void  handleRequest(Socket conn) {
        System.out.println("handleRequest:" + new Random().nextInt());
    }
}
class ThreadPerTaskExecutor implements Executor{
    @Override
    public void execute(Runnable command) {
        new Thread(command).start();
    }
}
class WithinThreadExecutor implements Executor{
    @Override
    public void execute(Runnable command) {
        command.run();

    }
}